package com.youmingxing.adpter;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.selfInfo.MyAuditionEndActivity;
import com.youmingxing.activity.selfInfo.MyAuditionGoingActivity;
import com.youmingxing.bean.AuditionFromUser;
import com.youmingxing.utils.ViewHolderUtils;

import java.util.List;

/**
 * 个人主页 海选
 */
public class PersonalInfoAuditionAdapter extends BaseAdapter {
    private List<AuditionFromUser> mList;
    private ImageView categoryImg;
    private TextView auditionName;
    private TextView category;
    private TextView voteNum;
    private TextView rank;
    private TextView status;
    private Context context;
    public int userId;

    public PersonalInfoAuditionAdapter(Context mContext) {
        this.context = mContext;
    }

    public void setData(List<AuditionFromUser> list) {
        if (list == null)
            return;
        if (mList == null) {
            mList = list;
        } else {
            mList.clear();
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        if ((null != mList && mList.size() > 0) && (position >= 0 && position < mList.size())) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final AuditionFromUser bean = mList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_personinfo_auditions_item, null);
        }
        categoryImg = ViewHolderUtils.getViewHolderView(convertView, R.id.category_img);
        auditionName = ViewHolderUtils.getViewHolderView(convertView, R.id.audition_name);
        category = ViewHolderUtils.getViewHolderView(convertView, R.id.category_name);
        voteNum = ViewHolderUtils.getViewHolderView(convertView, R.id.vote_num);
        rank = ViewHolderUtils.getViewHolderView(convertView, R.id.rank_tv);
        status = ViewHolderUtils.getViewHolderView(convertView, R.id.audition_status);
        Glide.with(context.getApplicationContext()).load(bean.category_img)
                .placeholder(R.drawable.default_16_9).error(R.drawable.default_16_9)
                .animate(R.anim.abc_fade_in).into(categoryImg);
        auditionName.setText(bean.audition_title);
        category.setText(bean.category);
        voteNum.setText("票数:" + bean.votes);
        rank.setText("排名:" + bean.rank);
        status.setText(bean.status_tip);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (bean.status == 0) {
                    intent = new Intent(context, MyAuditionGoingActivity.class);
                } else {
                    intent = new Intent(context, MyAuditionEndActivity.class);
                }
                intent.putExtra("id", bean.id);
                context.startActivity(intent);
            }
        });
        return convertView;
    }
}